package com.sniper.mybatis.service.impl;

import com.sniper.data.DataValues;
import com.sniper.exception.AlreadyExistsException;
import com.sniper.model.AdminGroup;
import com.sniper.model.AdminUser;
import com.sniper.model.AdminUserGroup;
import com.sniper.mybatis.dao.BaseDao;
import com.sniper.utils.FilesUtil;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("adminUserService")
public class AdminUserServiceImpl extends BaseServiceImpl<AdminUser> implements AdminUserService {

    @Resource
    AdminGroupService groupService;

    @Resource
    AdminUserGroupService adminUserGroupService;


    @Override
    @Resource
    public void setDao(BaseDao<AdminUser> dao) {
        super.setDao(dao);
    }

    @Override
    public AdminUser validateByName(String username) {
        Map<String, Object> params = new HashMap<>();
        params.put("name", username);
        List<AdminUser> adminUser = this.query("find", params);
        if (adminUser.size() > 0) {
            return adminUser.get(0);
        }
        return null;
    }

    @Override
    public AdminUser getUserAndGroupAndRight(String username) {
        AdminUser adminUser = this.get("get_user_by_name", username);
        if (adminUser == null) {
            return null;
        }
        List<AdminGroup> adminGroups = adminUser.getAdminGroup();
        if (adminGroups.size() > 0) {
            for (int i = 0; i < adminGroups.size(); i++) {
                AdminGroup adminGroup = groupService.get(adminGroups.get(i).getId());
                adminGroups.set(i, adminGroup);
            }
        }
        return adminUser;
    }

    @Override
    public AdminUser validateByNickName(String username) {
        Map<String, Object> params = new HashMap<>();
        params.put("nickName", username);
        List<AdminUser> adminUser = this.query("select", params);
        if (adminUser.size() > 0) {
            return adminUser.get(0);
        }
        return null;
    }

    @Override
    public AdminUser validateByNickName(String username, String notUser) {
        Map<String, Object> params = new HashMap<>();
        params.put("nickName", username);
        params.put("notUser", notUser);
        List<AdminUser> adminUser = this.query("select", params);
        if (adminUser.size() > 0) {
            return adminUser.get(0);
        }
        return null;
    }

    @Override
    public AdminUser validateByEmail(String email) {
        Map<String, Object> params = new HashMap<>();
        params.put("email", email);
        List<AdminUser> adminUser = this.query("select", params);
        if (adminUser.size() > 0) {
            return adminUser.get(0);
        }
        return null;
    }

    @Override
    public AdminUser validateByEmail(String email, String notUser) {
        Map<String, Object> params = new HashMap<>();
        params.put("email", email);
        params.put("notUser", notUser);
        List<AdminUser> adminUser = this.query("select", params);
        if (adminUser.size() > 0) {
            return adminUser.get(0);
        }
        return null;
    }

    @Override
    public List<AdminUser> listByEmail(String email) {
        Map<String, Object> params = new HashMap<>();
        params.put("email", email);
        return this.query("select", params);
    }

    @Override
    public boolean validateUser(String name, String password) {
        Map<String, Object> params = new HashMap<>();
        params.put("namePassword", name);
        params.put("password", password);
        AdminUser adminUser = (AdminUser) this.query("find", params);
        return adminUser != null;
    }


    @Override
    public int insert(AdminUser t) {
        int result = super.insert(t);
        List<AdminGroup> groups = t.getAdminGroup();
        if (groups.size() > 0) {
            for (AdminGroup adminGroup : groups) {
                AdminUserGroup adminUserGroup = new AdminUserGroup();
                adminUserGroup.setGid(adminGroup.getId());
                adminUserGroup.setUid(t.getId());
                adminUserGroupService.insert(adminUserGroup);
            }
        }
        return result;
    }

    @Override
    public int update(AdminUser t) {

        if (t.getAdminGroup().size() > 0) {
            String uid = t.getId();
            adminUserGroupService.delete("delete", uid);
            List<AdminGroup> groups = t.getAdminGroup();
            for (AdminGroup adminGroup : groups) {
                AdminUserGroup adminUserGroup = new AdminUserGroup();
                adminUserGroup.setGid(adminGroup.getId());
                adminUserGroup.setUid(uid);
                // 需要插入的用户组
                adminUserGroupService.insert(adminUserGroup);
            }
        }

        return super.update(t);
    }

    @Override
    public int delete(String id) {
        adminUserGroupService.delete(id);
        return super.delete(id);
    }


    /**
     * 注册前台用户，留言专用
     *
     * @param user
     * @return
     */
    @Override
    public AdminUser register(AdminUser user) {
        // 检测用户是否存在
        AdminUser adminUser = this.validateByName(user.getName());
        System.out.println(adminUser);
        if (adminUser != null) {
            throw new AlreadyExistsException("用户已存在");
        }
        // 执行插入操作
        user.setId(FilesUtil.UUID());
        user.setEnabled(true);
        user.setLocked(false);
        if (StringUtils.isEmpty(user.getNickName())) {
            user.setNickName(user.getName());
        }
        user.setPassword(DigestUtils.sha1Hex(user.getPassword()));
        //处室为空
        user.setSiteid("");
        // 插入用户组
        AdminGroup adminGroup = groupService.findGroup(DataValues.ROLE_GUEST);
        if (adminGroup != null) {
            user.setAdminGroup(Collections.singletonList(adminGroup));
        }
        // ch插入用户
        this.insert(user);
        return user;
    }

    /**
     * 注册前台用户
     *
     * @param user
     * @param password
     * @return
     */
    @Override
    public AdminUser regUser(AdminUser user, String password) {
        AdminGroup group = groupService.findGroup(DataValues.ROLE_GUEST);
        user.getAdminGroup().add(group);
        this.insert(user);
        if (this.insert(user) == 1) {
            return user;
        }
        return null;
    }

    /**
     * 获取用户这里获取的用户不能用于数据插入使用
     *
     * @param sourceUid
     * @return
     */
    @Override
    public AdminUser getUser(String sourceUid) {

        return this.get(sourceUid);
    }

    /**
     * 更改密码
     *
     * @param username
     * @param password 未加密
     * @return
     */
    @Override
    public int changePassword(String username, String password) {
        AdminUser adminUser = this.validateByName(username);
        if (adminUser != null) {
            adminUser.setPassword(DigestUtils.sha1Hex(password));
            return this.update(adminUser);
        }
        return 0;
    }

    @Override
    public int changePasswordById(String uid, String passwd) {
        AdminUser adminUser = get(uid);
        if (adminUser != null) {
            adminUser.setPassword(DigestUtils.sha1Hex(passwd));
            return this.update(adminUser);
        }
        return 0;
    }
}